package com.msa.msaportal.web.filter;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.msa.msaportal.common.web.ConstantUntil;
import com.msa.msaportal.common.web.MsaJsonUntil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class PortalFilter implements Filter{

	@Override
	public void destroy() {

		System.out.println("----过滤器销毁----");
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse resp = (HttpServletResponse)response;
        resp.setContentType("text/html");
        //String userCn = "350583198711282636";//req.getUserPrincipal().getName();
        String userCn = "120103197502204518";//req.getUserPrincipal().getName();
        //String userCn = "";//req.getUserPrincipal().getName();
        Set<String> roleSet=new HashSet<String>();
		
          
        Enumeration enumeration = req.getHeaderNames();
		while (enumeration.hasMoreElements()) {
			String head_name = (String) enumeration.nextElement();
			String value = req.getHeader(head_name);
			if (head_name.equals("OAM_REMOTE_USER")) {
				userCn = value;
			}
		}
		System.out.println("userCn:"+userCn);
		HttpSession session = req.getSession();
		
		if(session.getAttribute("loginUserId")!=null&&session.getAttribute("productRoleid")!=null){
			//System.out.println("已登录");
			chain.doFilter(request, response);
			
		}else if(!userCn.equals("") && userCn!=null){
			//System.out.println("未登录");
			String json = MsaJsonUntil.getJsonByUserId(userCn);
			JSONObject job = JSONObject.fromObject(json);
			//System.out.println(json);
			String productRoleId ="";
			JSONArray jsonarry = job.getJSONObject("datas").getJSONObject("table").getJSONObject("row").getJSONArray("table").getJSONObject(0).getJSONArray("row");
			for(int i=0;i<jsonarry.size();i++){
				
				if(ConstantUntil.APPID.equals(jsonarry.getJSONObject(i).getString("appId"))){
					productRoleId += jsonarry.getJSONObject(i).getString("productRoleId")+",";
				}
				roleSet.add(jsonarry.getJSONObject(i).getString("roleCode"));
			}
			System.out.println("role:::"+productRoleId);
			
			String orgCode = job.getJSONObject("datas").getJSONObject("table").getJSONObject("row").getString("organizations");
			String orgName=job.getJSONObject("datas").getJSONObject("table").getJSONObject("row").getString("orgName");
			String deptCode = job.getJSONObject("datas").getJSONObject("table").getJSONObject("row").getString("departments");
			
			JSONArray jsonDeptArray = job.getJSONObject("datas").getJSONObject("table").getJSONObject("row").getJSONArray("table").getJSONObject(2).getJSONArray("row");
			String deptName=jsonDeptArray.getJSONObject(0).getString("deptName");
			
			if(!"".equals(productRoleId)){
				//productRoleId = productRoleId.substring(0, productRoleId.length()-1);
				session.setAttribute("loginUserId", userCn);
				session.setAttribute("roleSet", roleSet);
				session.setAttribute("productRoleid", productRoleId);
				session.setAttribute("orgCode", orgCode);
				session.setAttribute("orgName", orgName);
				session.setAttribute("deptCode", deptCode);
				session.setAttribute("deptName", deptName);
				chain.doFilter(request, response);
			}else{
				resp.sendRedirect(ConstantUntil.LOGOUT_URL);
			}
			

		}else{
			resp.sendRedirect(ConstantUntil.LOGOUT_URL);
		}
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		 System.out.println("----过滤器初始化----");
	}

}
